using System.Collections.Generic;
using Game.Core;
using Game.Main.Model;
using Game.Main.View;
using Game.Manager;
using Game.XH;
using UnityEngine;

namespace Game.Component
{
	public class MedicineConveyorItem : BaseItem
	{
		private enum Anim
		{
			Static = 0,
			Running = 1
		}

		private Anim anim;

		private float animTimer;

		private Transform btn1;

		private Transform btn2;

		private Transform btn3;

		private Transform pillIcon1;

		private Transform pillIcon2;

		private Transform pillIcon3;

		private BaseItem createPill;

		private List<Transform> lines = new List<Transform>();

		private float startX;

		private float endX;

		private float lengthX;

		private float perX;

		private Transform pill1Trans;

		private string pill1Id;

		private Transform pill2Trans;

		private string pill2Id;

		private Transform pill3Trans;

		private string pill3Id;

		private float tempNewX;

		private float lastX;

		private float moveX;

		private float difX;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			btn1 = model.Find("Btn1");
			btn1.gameObject.layer = 15;
			btn2 = model.Find("Btn2");
			btn2.gameObject.layer = 15;
			btn3 = model.Find("Btn3");
			btn3.gameObject.layer = 15;
			Transform transform = backMask.Find("Conveyor");
			int childCount = transform.childCount;
			for (int i = 0; i < childCount; i++)
			{
				Transform child = transform.GetChild(i);
				lines.Add(child);
				if (i == 0)
				{
					startX = child.localPosition.x;
				}
				else if (i == childCount - 1)
				{
					endX = child.localPosition.x;
				}
			}
			lengthX = endX - startX;
			perX = lengthX / (float)(childCount - 1);
			pill1Trans = model.Find("1").GetChild(0);
			pill1Id = pill1Trans.name;
			pill2Trans = model.Find("2").GetChild(0);
			pill2Id = pill2Trans.name;
			pill3Trans = model.Find("3").GetChild(0);
			pill3Id = pill3Trans.name;
		}

		public override void ResetState()
		{
			base.ResetState();
			UpdatePillState();
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			if (anim != Anim.Running)
			{
				return;
			}
			animTimer += Time.deltaTime / 4f;
			moveX = animTimer * lengthX;
			for (int i = 0; i < lines.Count; i++)
			{
				Transform trans = lines[i];
				tempNewX = startX + perX * (float)i + moveX;
				if (tempNewX > endX)
				{
					tempNewX = startX + (tempNewX - endX);
				}
				trans.SetLocalX(tempNewX);
			}
			if (animTimer >= 1f)
			{
				animTimer = 0f;
				anim = Anim.Static;
			}
			difX = moveX - lastX;
			lastX = moveX;
			for (int j = 0; j < childItems.Count; j++)
			{
				BaseItem baseItem = childItems[j];
				if (baseItem != null)
				{
					tempNewX = baseItem.transform.localPosition.x + difX;
					if (tempNewX > 3.27f)
					{
						tempNewX = 3.27f;
					}
					baseItem.transform.SetLocalX(tempNewX);
				}
			}
		}

		public override void TouchDown(TouchEventData touchEventData)
		{
			base.TouchDown(touchEventData);
			RaycastHit2D raycastHit2D = Physics2D.Raycast(touchEventData.curPos, Vector2.zero, 15f, 32768);
			if (raycastHit2D.collider != null)
			{
				if (raycastHit2D.collider.transform == btn1)
				{
					SetBtnState(btn1, true);
					CreatePill(1);
				}
				else if (raycastHit2D.collider.transform == btn2)
				{
					SetBtnState(btn2, true);
					CreatePill(2);
				}
				else if (raycastHit2D.collider.transform == btn3)
				{
					SetBtnState(btn3, true);
					CreatePill(3);
				}
			}
		}

		private void CreatePill(int index)
		{
			if (anim != 0)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_error");
				return;
			}
			Singleton<AudioManager>.Instance.PlaySound("sound_btn_1");
			Transform transform = null;
			string empty = string.Empty;
			switch (index)
			{
			case 2:
				transform = pill2Trans;
				empty = pill2Id;
				break;
			case 3:
				transform = pill3Trans;
				empty = pill3Id;
				break;
			default:
				transform = pill1Trans;
				empty = pill1Id;
				break;
			}
			if (!transform.gameObject.activeSelf || GetCreateNum(empty) >= base.itemData.createAmount)
			{
				Singleton<AudioManager>.Instance.PlaySound("sound_error");
				return;
			}
			BaseItem baseItem = Singleton<Scene>.Instance.CreateItem(empty, model.Find("CreatePoint").position, 0);
			if (baseItem != null)
			{
				baseItem.PlayDragRotateAnim(true);
				AttachItem(baseItem, DepthType.Back, AttachType.Place, false);
				AddCreateItem(baseItem);
				createPill = baseItem;
				anim = Anim.Running;
				animTimer = 0f;
				depthSystem.SortBaseNode();
				lastX = 0f;
			}
		}

		public override void AddCreateItem(BaseItem item)
		{
			base.AddCreateItem(item);
			if (item != null)
			{
				UpdatePillState();
			}
		}

		public override void RemoveCreateItem(BaseItem item)
		{
			base.RemoveCreateItem(item);
			if (item != null)
			{
				UpdatePillState();
			}
		}

		private void UpdatePillState()
		{
			if (GetCreateNum(pill1Id) < base.itemData.createAmount)
			{
				pill1Trans.gameObject.SetActive(true);
			}
			else
			{
				pill1Trans.gameObject.SetActive(false);
			}
			if (GetCreateNum(pill2Id) < base.itemData.createAmount)
			{
				pill2Trans.gameObject.SetActive(true);
			}
			else
			{
				pill2Trans.gameObject.SetActive(false);
			}
			if (GetCreateNum(pill3Id) < base.itemData.createAmount)
			{
				pill3Trans.gameObject.SetActive(true);
			}
			else
			{
				pill3Trans.gameObject.SetActive(false);
			}
		}

		private int GetCreateNum(string id)
		{
			int num = 0;
			for (int i = 0; i < childItems.Count; i++)
			{
				if (childItems[i].itemData.id == id)
				{
					num++;
				}
			}
			return num;
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			SetBtnState(btn1, false);
			SetBtnState(btn2, false);
			SetBtnState(btn3, false);
		}

		private void SetBtnState(Transform trans, bool isDown)
		{
			trans.Find("Down").SetObjectActive(isDown);
			trans.Find("Up").SetObjectActive(!isDown);
		}
	}
}
